//
//  GSOptionView.m
//  GameSky
//
//  Created by lanou on 16/1/13.
//  Copyright © 2016年 NiRongfei. All rights reserved.
//

#import "GSOptionView.h"

#define kLabelWitdh 60
#define kLabelHeight 30
#define kMargin 10

#define kFont [UIFont systemFontOfSize:15]

@implementation GSOptionView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // 夜间模式
        self.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);
        
        for (int i = 0; i < 3; i++) {
            self.layer.cornerRadius = 5;
            self.layer.shadowRadius = 5;
            self.layer.shadowOpacity = 0.5;
            self.layer.shadowOffset = CGSizeMake(0, 0);
            self.layer.shadowColor = [UIColor blackColor].CGColor;
            
            // 循环添加左边三个Label;
            for (int i = 0; i < 3; i++) {
                UILabel *label = [[UILabel alloc] init];
                label.center = CGPointMake(kWidth / 4, kHeight / 6 + kHeight / 3 * i);
                label.bounds = CGRectMake(0, 0, kLabelWitdh, kLabelHeight);
                label.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor grayColor]);
                label.font = kFont;
                label.textAlignment = NSTextAlignmentLeft;
                label.tag = i + 10;
                [self addSubview:label];
            }
            UILabel *fontLabel = [self viewWithTag:10];
            fontLabel.text = @"字体大小";
            self.modeLabel = [self viewWithTag:11];
            self.modeLabel.text = @"夜间模式";
            self.imageLabel = [self viewWithTag:12];
            self.imageLabel.text = @"图片加载";
            
            // 循环添加字体大小按钮;
            for (int i = 0; i < 3; i++) {
                UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
                button.center = CGPointMake(kWidth * 0.75 - 20 + 20 * i, kHeight / 6);
                button.bounds = CGRectMake(0, 0, 20, kLabelHeight);
                button.titleLabel.font = kFont;
                button.tag = i + 20;
                [button addTarget:self action:@selector(fontButtonCLick:) forControlEvents:(UIControlEventTouchUpInside)];
                [self addSubview:button];
            }
            self.smallBtn = [self viewWithTag:20];
            [self.smallBtn setTitle:@"小" forState:(UIControlStateNormal)];
            [self.smallBtn setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
            self.midBtn = [self viewWithTag:21];
            [self.midBtn setTitle:@"中" forState:(UIControlStateNormal)];
            [self.midBtn setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
            self.bigBtn = [self viewWithTag:22];
            [self.bigBtn setTitle:@"大" forState:(UIControlStateNormal)];
            [self.bigBtn setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
            
            // 添加模式开关;
            self.modeSwitch = [[UISwitch alloc] init];
            self.modeSwitch.center = CGPointMake(kWidth * 0.75 + 5, kHeight / 2);
            self.modeSwitch.bounds = CGRectMake(0, 0, kLabelWitdh, kLabelHeight);
            self.modeSwitch.onTintColor = [UIColor grayColor];
            [self.modeSwitch addTarget:self action:@selector(modeSwitchClick:) forControlEvents:(UIControlEventValueChanged)];
            [self addSubview:self.modeSwitch];
            // 获得当前夜间模式状态设置模式开关;
            if ([[kUserDefaults valueForKey:@"nightMode"] isEqualToString:@"on"]) {
                _modeSwitch.on = YES;
            } else {
                _modeSwitch.on = NO;
            }

            // 添加图片加载开关;
            self.imageSwitch = [[UISwitch alloc] init];
            self.imageSwitch.center = CGPointMake(self.modeSwitch.center.x, kHeight * 5 / 6);
            self.imageSwitch.bounds = CGRectMake(0, 0, kLabelWitdh, kLabelHeight);
            self.imageSwitch.onTintColor = [UIColor grayColor];
            [self.imageSwitch addTarget:self action:@selector(imageSwitchClick:) forControlEvents:(UIControlEventValueChanged)];
            [self addSubview:self.imageSwitch];
            // 获得当前夜间模式状态设置模式开关;
            if ([[kUserDefaults valueForKey:@"picMode"] isEqualToString:@"on"]) {
                _imageSwitch.on = YES;
            } else {
                _imageSwitch.on = NO;
            }
        }
    }
    return self;
}
// 字体大小按钮点击方法;
- (void)fontButtonCLick:(UIButton *)button
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(setFontOfWebView:)]) {
        [self.delegate setFontOfWebView:button.tag];
    }
}
// 夜间模式开关点击方法;
- (void)modeSwitchClick:(UISwitch *)aSwitch
{
    if (aSwitch.on) {
        [kNotificationCenter postNotificationName:DKNightVersionNightFallingNotification object:self];
        [kUserDefaults setValue:@"on" forKey:@"nightMode"];
    } else {
        [kNotificationCenter postNotificationName:DKNightVersionDawnComingNotification object:self];
        [kUserDefaults setValue:@"off" forKey:@"nightMode"];
    }
}
// 图片模式开关点击方法;
- (void)imageSwitchClick:(UISwitch *)aSwitch
{
    if (aSwitch.on) {
        [kUserDefaults setValue:@"on" forKey:@"picMode"];
    } else {
        [kUserDefaults setValue:@"off" forKey:@"picMode"];    }

}

@end
